
package com.kzd.sdk.ui.fragment;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.kzd.sdk.base.OwnerFragment;
import com.kzd.sdk.core.KZD_SdkController;
import com.kzd.sdk.ui.activity.KZD_WebRichActivity;
import com.kzd.sdk.utils.AppUtils;

import static com.kzd.sdk.core.KZD_ApiConstance.KEY_USER_AGREE;

public class KZD_AccessAgreeFragment extends OwnerFragment {


    private TextView tvAccessAgreeExit, tvAccessAgreeSubmit, tvAccessAgreeHint, tvAccessAgreeContent;
    private CheckBox cbAccessAgreeSelect1, cbAccessAgreeSelect2;

    @Override
    public String getLayoutIdByName() {
        return "kzd_fragment_access_agree";
    }

    @Override
    public void initView(View view) {
        tvAccessAgreeHint = view.findViewById(AppUtils.getId(mContext, "tvAccessAgreeHint"));
        cbAccessAgreeSelect1 = view.findViewById(AppUtils.getId(mContext, "cbAccessAgreeSelect1"));
        cbAccessAgreeSelect2 = view.findViewById(AppUtils.getId(mContext, "cbAccessAgreeSelect2"));
        tvAccessAgreeExit = view.findViewById(AppUtils.getId(mContext, "tvAccessAgreeExit"));
        tvAccessAgreeSubmit = view.findViewById(AppUtils.getId(mContext, "tvAccessAgreeSubmit"));
        tvAccessAgreeContent = view.findViewById(AppUtils.getId(mContext, "tvAccessAgreeContent"));

        int resId = AppUtils.getStringId(mContext, "kzd_sdk_agree");
        if (resId > 0) {
            tvAccessAgreeContent.setText(resId);
        } else {
            String content = "GT手游专门为用户提供在线浏览，下载，上传服务的应用，为了保证用户正常使用，所以会获取访问网络、存储、电话等权限，并会在需要开启对应功能时，再提示您进行授权操作。";
            tvAccessAgreeContent.setText(content);
        }

        cbAccessAgreeSelect1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked && cbAccessAgreeSelect2.isChecked()) {
                    tvAccessAgreeSubmit.setBackgroundResource(AppUtils.getDrawableId(mContext, "kzd_back_50dp_blue"));
                    tvAccessAgreeSubmit.setEnabled(true);
                } else {
                    tvAccessAgreeSubmit.setBackgroundResource(AppUtils.getDrawableId(mContext, "kzd_back_50dp_gray"));
                    KZD_SdkController.getSp().put(KEY_USER_AGREE, false);
                    tvAccessAgreeSubmit.setEnabled(false);
                }
            }
        });

        cbAccessAgreeSelect2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked && cbAccessAgreeSelect1.isChecked()) {
                    tvAccessAgreeSubmit.setBackgroundResource(AppUtils.getDrawableId(mContext, "kzd_back_50dp_blue"));
                    tvAccessAgreeSubmit.setEnabled(true);
                } else {
                    tvAccessAgreeSubmit.setBackgroundResource(AppUtils.getDrawableId(mContext, "kzd_back_50dp_gray"));
                    KZD_SdkController.getSp().put(KEY_USER_AGREE, false);
                    tvAccessAgreeSubmit.setEnabled(false);
                }
            }
        });

        tvAccessAgreeExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KZD_SdkController.getSp().put(KEY_USER_AGREE, false);
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(v);
                }
            }
        });

        tvAccessAgreeSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KZD_SdkController.getSp().put(KEY_USER_AGREE, true);
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(v);
                }
            }
        });
    }

    @Override
    public void initData() {

        String agree = "选中以下选框即表示您确认已阅读并同意《用户协议》和《隐私协议》";
        SpannableString spannable = new SpannableString(agree);
        spannable.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                KZD_WebRichActivity.openApi("用户协议", "yhxy");
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#2983FF"));
                ds.setUnderlineText(false);
            }
        }, agree.indexOf("《"), agree.indexOf("》") + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                KZD_WebRichActivity.openApi("隐私协议", "yszc");
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#2983FF"));
                ds.setUnderlineText(false);
            }
        }, agree.lastIndexOf("《"), agree.lastIndexOf("》") + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tvAccessAgreeHint.setText(spannable);
        tvAccessAgreeHint.setMovementMethod(LinkMovementMethod.getInstance());
    }

}
